/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OrganizacijaPanel.java
 *
 * Created on Nov 17, 2010, 9:06:25 PM
 */

package hraplication;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Nermin
 */
public class UnosOrganizacioneJedinicePanel extends Paneli {

    Konekcija kon;
    /** Creates new form OrganizacijaPanel */
    public UnosOrganizacioneJedinicePanel(Konekcija konekcija) {
        initComponents();
        kon = konekcija;
        OsvjeziChBOX();

    }
    @Override
    public void OsvjeziChBOX(){
        jComboBox1.removeAllItems();
        //Popunjavanje prvog comboboxa
        ResultSet r = kon.Select("SELECT Grad FROM Gradovi");
        if (r!=null){
            try{
                while (r.next()){
                    jComboBox1.addItem(r.getString("Grad"));
                }
            }
            catch (SQLException ex)
            {
              System.err.println(ex.getMessage());
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hraplication.HRaplicationApp.class).getContext().getResourceMap(UnosOrganizacioneJedinicePanel.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextField1.setName("jTextField1"); // NOI18N

        jComboBox1.setName("jComboBox1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(hraplication.HRaplicationApp.class).getContext().getActionMap(UnosOrganizacioneJedinicePanel.class, this);
        jButton1.setAction(actionMap.get("Unesi")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleName(resourceMap.getString("Form.AccessibleContext.accessibleName")); // NOI18N
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        if (Provjeri()){
            Ubaci();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private Boolean Provjeri(){
        String ime = jTextField1.getText();

        //Provjere ispravnosti podataka
        if (ime.equals("")){
            JOptionPane.showMessageDialog(this, "Unesite naziv organizacione jedinice!", "Nedostaje podatak",WIDTH,null);
            return false;
        }
        return true;
    }

    private void Ubaci() {
        int id_grad=-1;

        try{
            ResultSet r = kon.Select("SELECT idGradovi FROM Gradovi WHERE Grad = '"+jComboBox1.getSelectedItem().toString()+"'");
            if (r.next()){
                id_grad = r.getInt(1);
            }

            String ins = "INSERT INTO organizacione_jedinice (Gradovi_idGradovi, Naziv) VALUES(";
            ins=ins+id_grad+",'"+jTextField1.getText()+"')";
            //System.err.println(ins);
            String greska = kon.Insert(ins);
            if (greska != null)
                //DOSLO DO GRESKE
                JOptionPane.showMessageDialog(this, greska, "Greska",WIDTH,null);
            else{
                jTextField1.setText("");
            }
        }
        catch (SQLException ex)
        {
           System.err.println(ex.getMessage());
        }

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
